<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=A7V6AuDIaW67hNF5AY3u1RAexNMbaGuq"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script>
    <title>热力图</title>
    <style type="text/css">
		ul,li{list-style: none;margin:0;padding:0;float:left;}
		html{height:100%}
		body{height:100%;margin:0px;padding:0px;font-family:"微软雅黑";}
		#container{height:500px;width:100%;}
		#r-result{width:100%;}
    </style>	
</head>
<body>
	<div id="container"></div>
	<div id="r-result">
		<input type="button"  onclick="openHeatmap();" value="显示热力图"/><input type="button"  onclick="closeHeatmap();" value="关闭热力图"/>
	</div>
</body>
</html>
<script type="text/javascript">
    var map = new BMap.Map("container");          // 创建地图 

    var point = new BMap.Point(104.114129, 37.550339);
    map.centerAndZoom(point, 5);             
    map.enableScrollWheelZoom(); 
  
    var points =[
    {"lat":39.9299857781,"lng":116.395645038,"count":102},{"lat":39.1439299033,"lng":117.210813092,"count":101.8},{"lat":38.0489583146,"lng":114.522081844,"count":102.2},{"lat":39.6505309225,"lng":118.183450598,"count":0},{"lat":39.9454615659,"lng":119.604367616,"count":0},{"lat":37.890277054,"lng":112.550863589,"count":101.6},{"lat":40.8283188731,"lng":111.66035052,"count":101.6},{"lat":40.6471194257,"lng":109.846238532,"count":0},{"lat":41.8086447835,"lng":123.432790922,"count":102.9},{"lat":38.9487099383,"lng":121.593477781,"count":103.8},{"lat":40.1290228266,"lng":124.338543115,"count":0},{"lat":41.1308788759,"lng":121.147748738,"count":0},{"lat":43.8983376071,"lng":125.313642427,"count":101.4},{"lat":43.8719883344,"lng":126.564543989,"count":0},{"lat":45.7732246332,"lng":126.657716855,"count":102.1},{"lat":44.5885211528,"lng":129.608035396,"count":0},{"lat":31.24916171,"lng":121.487899486,"count":101.2},{"lat":32.0572355018,"lng":118.778074408,"count":101.1},{"lat":31.5700374519,"lng":120.305455901,"count":0},{"lat":34.2715534311,"lng":117.188106623,"count":0},{"lat":32.4085052546,"lng":119.427777551,"count":0},{"lat":30.2592444615,"lng":120.219375416,"count":102.7},{"lat":29.8852589659,"lng":121.579005973,"count":101.9},{"lat":28.002837594,"lng":120.690634734,"count":0},{"lat":29.1028991054,"lng":119.652575704,"count":0},{"lat":31.8669422607,"lng":117.282699092,"count":100.9},{"lat":32.9294989067,"lng":117.357079866,"count":0},{"lat":30.5378978174,"lng":117.058738772,"count":0},{"lat":26.0471254966,"lng":119.330221107,"count":100.8},{"lat":24.4892306125,"lng":118.103886046,"count":102.6},{"lat":24.901652384,"lng":118.600362343,"count":0},{"lat":28.6895780001,"lng":115.893527546,"count":102},{"lat":29.7196395261,"lng":115.999848022,"count":0},{"lat":25.8452955363,"lng":114.935909079,"count":0},{"lat":36.6827847272,"lng":117.024967066,"count":101.8},{"lat":36.1052149013,"lng":120.384428184,"count":102.6},{"lat":37.5365615629,"lng":121.30955503,"count":0},{"lat":35.4021216643,"lng":116.600797625,"count":0},{"lat":34.7566100641,"lng":113.64964385,"count":102.7},{"lat":34.6573678177,"lng":112.447524769,"count":0},{"lat":33.7453014565,"lng":113.300848978,"count":0},{"lat":30.5810841269,"lng":114.316200103,"count":102},{"lat":30.732757818,"lng":111.310981092,"count":0},{"lat":32.2291685915,"lng":112.250092848,"count":0},{"lat":28.2134782309,"lng":112.979352788,"count":100.2},{"lat":29.3780070755,"lng":113.146195519,"count":0},{"lat":29.0121488552,"lng":111.653718137,"count":0},{"lat":23.1200491021,"lng":113.307649675,"count":102.1},{"lat":24.8029603119,"lng":113.594461107,"count":0},{"lat":22.5460535462,"lng":114.025973657,"count":101.5},{"lat":21.2574631038,"lng":110.365067263,"count":0},{"lat":23.1135398524,"lng":114.41065808,"count":0},{"lat":22.8064929356,"lng":108.297233556,"count":102.5},{"lat":25.262901246,"lng":110.260920147,"count":0},{"lat":21.472718235,"lng":109.122627919,"count":0},{"lat":20.022071277,"lng":110.330801848,"count":102.9},{"lat":18.2577759149,"lng":109.522771281,"count":0},{"lat":29.5446061089,"lng":106.530635013,"count":101.6},{"lat":30.6799428454,"lng":104.067923463,"count":101.3},{"lat":28.8959298039,"lng":105.443970289,"count":0},{"lat":30.8009651682,"lng":106.105553984,"count":0},{"lat":26.6299067414,"lng":106.709177096,"count":101.2},{"lat":27.6999613771,"lng":106.931260316,"count":0},{"lat":25.0491531005,"lng":102.714601139,"count":100.6},{"lat":25.6939666225,"lng":100.219208954,"count":0},{"lat":42.9863649464,"lng":125.150148579,"count":102.5},{"lat":36.064225525,"lng":103.823305441,"count":102.9},{"lat":36.640738612,"lng":101.76792099,"count":102.4},{"lat":38.5026210119,"lng":106.206478608,"count":102.5},{"lat":43.8403803472,"lng":87.5649877411,"count":103.8}];
   
    if(!isSupportCanvas()){
    	alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能')
    }
	//详细的参数,可以查看heatmap.js的文档 https://github.com/pa7/heatmap.js/blob/master/README.md
	//参数说明如下:
	/* visible 热力图是否显示,默认为true
     * opacity 热力的透明度,1-100
     * radius 势力图的每个点的半径大小   
     * gradient  {JSON} 热力图的渐变区间 . gradient如下所示
     *	{
			.2:'rgb(0, 255, 255)',
			.5:'rgb(0, 110, 255)',
			.8:'rgb(100, 0, 255)'
		}
		其中 key 表示插值的位置, 0~1. 
		    value 为颜色值. 
     */
	heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":23});
	map.addOverlay(heatmapOverlay);
	heatmapOverlay.setDataSet({data:points,max:110});
	//是否显示热力图
    function openHeatmap(){
        heatmapOverlay.show();
    }
	function closeHeatmap(){
        heatmapOverlay.hide();
    }
	closeHeatmap();
    function setGradient(){
     	/*格式如下所示:
		{
	  		0:'rgb(102, 255, 0)',
	 	 	.5:'rgb(255, 170, 0)',
		  	1:'rgb(255, 0, 0)'
		}*/
     	var gradient = {};
     	var colors = document.querySelectorAll("input[type='color']");
     	colors = [].slice.call(colors,0);
     	colors.forEach(function(ele){
			gradient[ele.getAttribute("data-key")] = ele.value; 
     	});
        heatmapOverlay.setOptions({"gradient":gradient});
    }
	//判断浏览区是否支持canvas
    function isSupportCanvas(){
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }
</script>